rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)
library(ggplot2)

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, memrableperiod_pret := memrableperiod_pret * 100]
m[, memrableperiod_aret := memrableperiod_aret * 100]
m[, memrableperiod_ownret := memrableperiod_ownret * 100]
m[, retrecall_1day_rate := retrecall_1day_rate * 100]
m[, retrecall_30day_rate := retrecall_30day_rate * 100]
m[, retrecall_1year_rate := retrecall_1year_rate * 100]
m[, retrecall_5year_rate := retrecall_5year_rate * 100]

m[, expret30day_market_rate := expret30day_market_rate * 100]
m[, expret30day_self_rate := expret30day_self_rate * 100]
m[, expret1year_market_rate := expret1year_market_rate * 100]
m[, expret1year_self_rate := expret1year_self_rate * 100]

summ <- NULL

# A1
summ <- rbind(summ, m[!is.na(memrableperiod_pret), {
  list(
    N = .N,
    mean = mean(memrableperiod_pret),
    sd = sd(memrableperiod_pret),
    p5 = quantile(memrableperiod_pret, 0.05),
    p25 = quantile(memrableperiod_pret, 0.25),
    p50 = median(memrableperiod_pret),
    p75 = quantile(memrableperiod_pret, 0.75),
    p95 = quantile(memrableperiod_pret, 0.95)
  )
}])

summ <- rbind(summ, m[type == 0 & !is.na(memrableperiod_pret), {
  list(
    N = .N,
    mean = mean(memrableperiod_pret),
    sd = sd(memrableperiod_pret),
    p5 = quantile(memrableperiod_pret, 0.05),
    p25 = quantile(memrableperiod_pret, 0.25),
    p50 = median(memrableperiod_pret),
    p75 = quantile(memrableperiod_pret, 0.75),
    p95 = quantile(memrableperiod_pret, 0.95)
  )
}])

# type == 1
summ <- rbind(summ, m[type == 1 & !is.na(memrableperiod_pret), {
  list(
    N = .N,
    mean = mean(memrableperiod_pret),
    sd = sd(memrableperiod_pret),
    p5 = quantile(memrableperiod_pret, 0.05),
    p25 = quantile(memrableperiod_pret, 0.25),
    p50 = median(memrableperiod_pret),
    p75 = quantile(memrableperiod_pret, 0.75),
    p95 = quantile(memrableperiod_pret, 0.95)
  )
}])

# type == 2
summ <- rbind(summ, m[type == 2 & !is.na(memrableperiod_pret), {
  list(
    N = .N,
    mean = mean(memrableperiod_pret),
    sd = sd(memrableperiod_pret),
    p5 = quantile(memrableperiod_pret, 0.05),
    p25 = quantile(memrableperiod_pret, 0.25),
    p50 = median(memrableperiod_pret),
    p75 = quantile(memrableperiod_pret, 0.75),
    p95 = quantile(memrableperiod_pret, 0.95)
  )
}])

# A2 Probed recall
summ <- rbind(summ, m[!is.na(retrecall_1day_rate), {
  list(
    N = .N,
    mean = mean(retrecall_1day_rate),
    sd = sd(retrecall_1day_rate),
    p5 = quantile(retrecall_1day_rate, 0.05),
    p25 = quantile(retrecall_1day_rate, 0.25),
    p50 = median(retrecall_1day_rate),
    p75 = quantile(retrecall_1day_rate, 0.75),
    p95 = quantile(retrecall_1day_rate, 0.95)
  )
}])

summ <- rbind(summ, m[!is.na(retrecall_30day_rate), {
  list(
    N = .N,
    mean = mean(retrecall_30day_rate),
    sd = sd(retrecall_30day_rate),
    p5 = quantile(retrecall_30day_rate, 0.05),
    p25 = quantile(retrecall_30day_rate, 0.25),
    p50 = median(retrecall_30day_rate),
    p75 = quantile(retrecall_30day_rate, 0.75),
    p95 = quantile(retrecall_30day_rate, 0.95)
  )
}])

summ <- rbind(summ, m[!is.na(retrecall_1year_rate), {
  list(
    N = .N,
    mean = mean(retrecall_1year_rate),
    sd = sd(retrecall_1year_rate),
    p5 = quantile(retrecall_1year_rate, 0.05),
    p25 = quantile(retrecall_1year_rate, 0.25),
    p50 = median(retrecall_1year_rate),
    p75 = quantile(retrecall_1year_rate, 0.75),
    p95 = quantile(retrecall_1year_rate, 0.95)
  )
}])

summ <- rbind(summ, m[!is.na(retrecall_5year_rate), {
  list(
    N = .N,
    mean = mean(retrecall_5year_rate),
    sd = sd(retrecall_5year_rate),
    p5 = quantile(retrecall_5year_rate, 0.05),
    p25 = quantile(retrecall_5year_rate, 0.25),
    p50 = median(retrecall_5year_rate),
    p75 = quantile(retrecall_5year_rate, 0.75),
    p95 = quantile(retrecall_5year_rate, 0.95)
  )
}])

# A3 Expectation
summ <- rbind(summ, m[!is.na(expret30day_market_rate), {
  list(
    N = .N,
    mean = mean(expret30day_market_rate),
    sd = sd(expret30day_market_rate),
    p5 = quantile(expret30day_market_rate, 0.05),
    p25 = quantile(expret30day_market_rate, 0.25),
    p50 = median(expret30day_market_rate),
    p75 = quantile(expret30day_market_rate, 0.75),
    p95 = quantile(expret30day_market_rate, 0.95)
  )
}])

summ <- rbind(summ, m[!is.na(expret1year_market_rate), {
  list(
    N = .N,
    mean = mean(expret1year_market_rate),
    sd = sd(expret1year_market_rate),
    p5 = quantile(expret1year_market_rate, 0.05),
    p25 = quantile(expret1year_market_rate, 0.25),
    p50 = median(expret1year_market_rate),
    p75 = quantile(expret1year_market_rate, 0.75),
    p95 = quantile(expret1year_market_rate, 0.95)
  )
}])

summ <- rbind(summ, m[!is.na(expret30day_self_rate), {
  list(
    N = .N,
    mean = mean(expret30day_self_rate),
    sd = sd(expret30day_self_rate),
    p5 = quantile(expret30day_self_rate, 0.05),
    p25 = quantile(expret30day_self_rate, 0.25),
    p50 = median(expret30day_self_rate),
    p75 = quantile(expret30day_self_rate, 0.75),
    p95 = quantile(expret30day_self_rate, 0.95)
  )
}])

summ <- rbind(summ, m[!is.na(expret1year_self_rate), {
  list(
    N = .N,
    mean = mean(expret1year_self_rate),
    sd = sd(expret1year_self_rate),
    p5 = quantile(expret1year_self_rate, 0.05),
    p25 = quantile(expret1year_self_rate, 0.25),
    p50 = median(expret1year_self_rate),
    p75 = quantile(expret1year_self_rate, 0.75),
    p95 = quantile(expret1year_self_rate, 0.95)
  )
}])

print.xtable(xtable(summ, digits = 1), include.rownames = F)

# Panel B: Merged sample
C <- data.table(read_xlsx("../Data/AccountData.xlsx", guess_max = 1e4))
C[, aret_1day_v2 := as.numeric(aret_1day_v2) * 100]
C[, aret_4_1week_v2 := as.numeric(aret_4_1week_v2) * 100]
C[, aret_ytd_v2 := as.numeric(aret_ytd_v2) * 100]
m <- C

summ <- data.table()

summ <- rbind(summ, m[!is.na(aret_1day_v2), {
  list(
    N = .N,
    mean = mean(aret_1day_v2),
    sd = sd(aret_1day_v2),
    p5 = quantile(aret_1day_v2, 0.05),
    p25 = quantile(aret_1day_v2, 0.25),
    p50 = median(aret_1day_v2),
    p75 = quantile(aret_1day_v2, 0.75),
    p95 = quantile(aret_1day_v2, 0.95)
  )
}])

summ <- rbind(summ, m[!is.na(aret_4_1week_v2), {
  list(
    N = .N,
    mean = mean(aret_4_1week_v2),
    sd = sd(aret_4_1week_v2),
    p5 = quantile(aret_4_1week_v2, 0.05),
    p25 = quantile(aret_4_1week_v2, 0.25),
    p50 = median(aret_4_1week_v2),
    p75 = quantile(aret_4_1week_v2, 0.75),
    p95 = quantile(aret_4_1week_v2, 0.95)
  )
}])

summ <- rbind(summ, m[!is.na(aret_ytd_v2), {
  list(
    N = .N,
    mean = mean(aret_ytd_v2),
    sd = sd(aret_ytd_v2),
    p5 = quantile(aret_ytd_v2, 0.05),
    p25 = quantile(aret_ytd_v2, 0.25),
    p50 = median(aret_ytd_v2),
    p75 = quantile(aret_ytd_v2, 0.75),
    p95 = quantile(aret_ytd_v2, 0.95)
  )
}])

summ[, mean := sprintf("%.1f%%", mean)]
summ[, sd := sprintf("%.1f%%", sd)]
summ[, p5 := sprintf("%.1f%%", p5)]
summ[, p25 := sprintf("%.1f%%", p25)]
summ[, p50 := sprintf("%.1f%%", p50)]
summ[, p75 := sprintf("%.1f%%", p75)]
summ[, p95 := sprintf("%.1f%%", p95)]

print.xtable(xtable(summ, digits = 1), include.rownames = F)

